import pymysql
import logging

class DBUtil_a(object):

    conn = None
    @classmethod
    def __get_con(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", port=7061, user="student",
                                           password="iHRM_student_2022", database="ihrm", charset="utf8")

        return cls.conn
    @classmethod
    def __close_conn(cls):
        if cls.conn is not None:
            cls.conn.close()
            cls.conn = None
        return cls.conn

    @classmethod
    def select_one(cls,sql):
        cursor = None
        res = None
        try:
            cls.conn = cls.__get_con()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
        except Exception as err:
            print(err)
        finally:
            cls.__close_conn()
            cursor.close()
            return res
    @classmethod
    def uid_db(cls,sql):
        cursor = None

        try:
            cls.conn = cls.__get_con()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            cls.conn.commit()
            # print(cls.conn.affected_rows())
            logging.info(f"影响的行数：{cls.conn.affected_rows()}")
        except Exception as err:
            print(err)
            cls.conn.rollback()
        finally:
            cursor.close()
            cls.__close_conn()


if __name__ == '__main__':
    res = DBUtil_a.select_one("select * from bs_user;")
    print("查询结果为：", res)
    # DBUtil.uid_db("update t_book set is_delete = 1 where id = 4;")